package cn.yeyiu.netonlinevideo.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.yeyiu.netonlinevideo.R;
import cn.yeyiu.util.AndroidUtil;
import cn.yeyiu.util.bean.BlogInfo;
import cn.yeyiu.util.bean.CommentInfo;
import cn.yeyiu.util.bean.ViewInfo;

public class BlogViewActivity extends AppCompatActivity {

    private ImageView backBtn;
    private TextView backTv;
    private ImageView magnetShare;
    private Spinner magnetSpinner;
    private Button magnetCopy;

    private LinearLayout containerLLa;
    private LinearLayout commentLLa;
    private DrawerLayout commentDrawer;
    private View floatComment;

    private BlogInfo blogInfo;
    ArrayAdapter<String> magnetsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_blog_view);

        dataInit();
        viewInit();
        eventInit();
        uiInit();
    }

    void dataInit() {
        blogInfo = JSONObject.parseObject(getIntent().getStringExtra("blog_info"), BlogInfo.class);
        magnetsAdapter = new ArrayAdapter<>(this, R.layout.spinner_item);

    }

    void viewInit() {
        backBtn = findViewById(R.id.back_btn);
        backTv = findViewById(R.id.back_tv);
        magnetShare = findViewById(R.id.magnet_share);
        magnetSpinner = findViewById(R.id.magnet_spinner);
        magnetCopy = findViewById(R.id.magnet_copy_btn);

        containerLLa = findViewById(R.id.blog_container_lla);
        commentLLa = findViewById(R.id.blog_comment_lla);
        commentDrawer = findViewById(R.id.blog_comment_drawer);
        floatComment = findViewById(R.id.blog_float_comment);

        magnetSpinner.setAdapter(magnetsAdapter);
    }

    void eventInit() {
        backBtn.setOnClickListener(v -> {
            finish();
        });
        magnetShare.setOnClickListener(v->{
            AndroidUtil.openInBrowser(BlogViewActivity.this, blogInfo.url);
        });
        magnetCopy.setOnClickListener((v) -> {
            String magnet = magnetSpinner.getSelectedItem().toString();
            AndroidUtil.copy(BlogViewActivity.this, magnet);
        });
        floatComment.setOnClickListener(v -> {
            commentDrawer.openDrawer(GravityCompat.END);
        });
    }

    void uiInit() {
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        // title
        backTv.setText(blogInfo.title);
        // header
        TextView header = new TextView(this);
        header.setText(blogInfo.title);
        header.setTextColor(getColor(R.color.main_color));
        header.setTextSize(24);
        header.setGravity(Gravity.CENTER_HORIZONTAL);
        params.setMargins(8, 8, 8, 8);
        header.setLayoutParams(params);
        containerLLa.addView(header);

        // spinner
        magnetsAdapter.add("Sexyiu");

        // view
        LinearLayout.LayoutParams hrParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1);
        hrParams.setMargins(0, 30, 0, 30);
        for (ViewInfo viewInfo : blogInfo.viewList) {

            TextView textView;
            switch (viewInfo.viewName) {
                case "h1":
                case "h2":
                    textView = new TextView(this);
                    textView.setText(viewInfo.text);
                    textView.setTextSize(20);
                    textView.setTextColor(getColor(R.color.black));

                    containerLLa.addView(textView);
                    break;

                case "#text":
                    textView = new TextView(this);
                    textView.setText(viewInfo.text);
                    textView.setTextSize(viewInfo.textSize);

                    if (viewInfo.textColor != -1) {
                        textView.setTextColor(viewInfo.textColor);
                    }
                    containerLLa.addView(textView);
                    break;

                case "img":
                    ImageView imageView = new ImageView(this);
                    imageView.setLayoutParams(params);
                    containerLLa.addView(imageView);
                    Glide.with(this).load(viewInfo.url).into(imageView);
                    break;

                case "hr":
                    View hr = new View(this);
                    hr.setLayoutParams(hrParams);
                    hr.setBackgroundColor(getColor(R.color.black));
                    containerLLa.addView(hr);
                    break;

                default:
                    System.out.println("未知元素:" + viewInfo.viewName);
                    break;
            }
            // 遍历 viewinfo 查找 magnet
            if (viewInfo.text != null) {
                Matcher m = Pattern.compile("[0-9a-zA-Z]{40}").matcher(viewInfo.text);
                if (m.find()) {
                    magnetsAdapter.add(viewInfo.text);
                }
            }
        }

        // comment
        for (CommentInfo commentInfo : blogInfo.commentList) {
            commentLLa.addView(initComment(commentInfo));

            View hr = new View(this);
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 4);
            params1.setMargins(0, 30, 0, 30);
            hr.setLayoutParams(params1);
            hr.setBackgroundColor(getColor(R.color.main_color));
            commentLLa.addView(hr);
        }

    }

    View initComment(CommentInfo commentInfo) {
        // root
        View commentView = LayoutInflater.from(this).inflate(R.layout.item_blog_comment, null);
        TextView name = commentView.findViewById(R.id.blog_comment_name);
        TextView time = commentView.findViewById(R.id.blog_comment_time);
        ImageView img = commentView.findViewById(R.id.blog_comment_img);
        TextView comment = commentView.findViewById(R.id.blog_comment);

        name.setText(commentInfo.name);
        time.setText(commentInfo.time);
        Glide.with(this).load(commentInfo.header).placeholder(R.drawable.pic_logo).into(img);
        comment.setText(commentInfo.comment);

        if (commentInfo.subCommentList == null) {
            return commentView;
        }

        // sub comment
        LinearLayout subCommentView = commentView.findViewById(R.id.blog_comment_sub_comment);

        for (CommentInfo subComment : commentInfo.subCommentList) {
            View subView = initComment(subComment);
            subCommentView.addView(subView);
        }
        return commentView;
    }
}